package leetcode.array;

public class Test59 {

    public static void main(String[] args) {
        int[][] ints = generateMatrix(3);
        for (int i = 0; i < ints.length; i++) {
            for (int j = 0;j<ints.length;j++){
                System.out.print(ints[i][j]+" ");
            }
            System.out.println();
        }
    }
    public static int[][] generateMatrix(int n) {
        int[][] r = new int[n][n];
        int x=0,y=0;
        int value = 0;
//        System.out.println(x+","+y);
        for (int i = n-1; i > 0; i-=2) {
            for (int t=1;t<=i;t++){
                    r[x][y] = ++value;
                    y++;
                System.out.println(x+","+y);
            }
            for (int t=1;t<=i;t++){
                r[x][y] = ++value;
                x++;
                System.out.println(x+","+y);
            }
            for (int t=1;t<=i;t++){
                r[x][y] = ++value;
                y--;
                System.out.println(x+","+y);
            }
            for (int t=1;t<=i;t++){
                r[x][y] = ++value;
                if(t==i){
                    y++;
                }
                else{
                 x--;
                }
//                System.out.println(x+","+y);
            }

        }
        if (n % 2 == 1){
            r[n/2][n/2] = n*n;
        }
        return r;
    }
}
